# -*- coding: utf-8 -*-

from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.core import urlresolvers
from django.conf import settings

class Base(models.Model):
    
    CH_TYPE_BONUS = (
        ('A', _(u'Ataque')),
        ('D', _(u'Defensa')),
        ('S', _(u'Velocidad')),
        ('P', _(u'Producción')),
    )

    player = models.ForeignKey('game.Player', verbose_name=_(u"Player"), blank=True, null=True, related_name='bases')
    x_coordinate = models.PositiveSmallIntegerField(verbose_name=_(u'X Coordinate'), blank=False, null=False)
    y_coordinate = models.PositiveSmallIntegerField(verbose_name=_(u'Y Coordinate'), blank=False, null=False)
    resources = models.FloatField(verbose_name=_(u'Number of resources'), blank=True, null=True, default=4000)
    game = models.ForeignKey('game.Game', verbose_name="Game", blank=False, null=False, related_name='bases')
    level = models.PositiveIntegerField(verbose_name=_(u'Level'), blank=False, null=False, default=1)
    bonus = models.CharField(verbose_name=_(u'Bonus'), max_length=4, blank=True, null=True, choices=CH_TYPE_BONUS)
    resources_limit = models.FloatField(verbose_name=_(u'Number of resources'), blank=True, null=True, default=80000)

    class Meta:
        app_label = 'game'
        verbose_name = _(u'Base')
        verbose_name_plural = _(u'Bases')

    def __unicode__(self):
        return unicode(self.id) + u' -- ' + unicode(self.game.id) + u' -- (' + unicode(self.x_coordinate) + u', ' + unicode(self.y_coordinate) + u') -- ' + unicode(self.player)


    def is_base_over(self):
        if self.resources == 0 and self.ants_base.count() == 0:
            return True
        else:
            return False


    def get_production(self):
        """
        Get base production in a minute.
        """

        ants = self.ants_base.filter(position='C')
        count = 0
        count+=ants.filter(type_ant='W').count()*settings.RESOURCES_COLLECTED[0]
        count+=ants.filter(type_ant='OO').count()*settings.RESOURCES_COLLECTED[1]
        count+=ants.filter(type_ant='OD').count()*settings.RESOURCES_COLLECTED[2]
        count+=ants.filter(type_ant='FO').count()*settings.RESOURCES_COLLECTED[3]
        count+=ants.filter(type_ant='FD').count()*settings.RESOURCES_COLLECTED[4]
        # count += ants.filter(type_ant = 'Q').count() * settings.RESOURCES_COLLECTED[5]
        
        bonus = 0
        # Player Bonus o Hormiguero Bonus
        # No son acumulativos

        if self.player.bonus == 'P' or self.player.bases.filter(bonus = 'P').exists():
            bonus += count*0.1
        """
        if self.player.bases.filter(bonus = 'P').exists(): # Hormiguero Bonus
            bonus += count*0.1
        """

        count += bonus
        return count

    def get_ants(self):
        ants = self.ants_base.filter(player = self.player, position = 'C')
        num_ants = []
        num_ants.append(ants.filter(type_ant = 'W').count())
        num_ants.append(ants.filter(type_ant = 'OO').count())
        num_ants.append(ants.filter(type_ant = 'OD').count())
        num_ants.append(ants.filter(type_ant = 'FO').count())
        num_ants.append(ants.filter(type_ant = 'FD').count())
        num_ants.append(ants.filter(type_ant = 'Q').count())
        return num_ants

    def get_coordinates_string(self):
        return u'(' + unicode(self.x_coordinate) + u', ' + unicode(self.y_coordinate) + u')'

    #def save(self, *args, **kwargs):
        """
        if not self.id:

            # Excepción innecesaria, jamás la provocaría el usuario
            for base in self.game.bases.all():
                if self.x_coordinate == base.x_coordinate and self.y_coordinate == base.y_coordinate:
                    raise Exception(u'No puede haber 2 bases con las mismas coordenadas, en el mismo juego.')
        """
    #super(Base, self).save(*args, **kwargs)

